package com.ec.system.modules.sys.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ec.common.constant.CommonConstant;
import com.ec.core.common.core.base.BasePageVO;
import com.ec.core.mybatis.utils.CorePageUtil;
import com.ec.core.secure.utils.SecureUtil;
import com.ec.system.modules.sys.dto.QueryPostDTO;
import com.ec.system.modules.sys.dto.SysPostDTO;
import com.ec.system.modules.sys.entity.SysPost;
import com.ec.system.modules.sys.mapper.SysPostMapper;
import com.ec.system.modules.sys.service.ISysPostService;
import com.ec.system.modules.sys.vo.SysPostVO;
import com.google.common.collect.Lists;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;


/**
 * @description:
 * @author: Eric Lee
 * @time: 2021/9/27 14:22
 */
@Service
public class SysPostServiceImpl extends ServiceImpl<SysPostMapper, SysPost> implements ISysPostService {

    @Override
    public BasePageVO<SysPostVO> listPost(QueryPostDTO queryPostDTO) {
        List<SysPostVO> list = Lists.newArrayList();
        if (Objects.isNull(queryPostDTO.getTenantId())) {
            queryPostDTO.setTenantId(SecureUtil.userTenantId());
        } else {
            queryPostDTO.setTenantId(queryPostDTO.getTenantId());
        }
        int count = baseMapper.listPostCount(queryPostDTO);
        if (count > 0) {
            list = baseMapper.listPost(queryPostDTO);
        }
        return CorePageUtil.buildPageResult(list, count, SysPostVO.class);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean addPost(SysPostDTO postDTO) {
        SysPost SysPost = new SysPost();
        postDTO.setPostCode(postDTO.getPostCode().toUpperCase());
        BeanUtil.copyProperties(postDTO, SysPost);
        SysPost.setTenantId(SecureUtil.userTenantId());
        return this.save(SysPost);
    }


    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean deletePost(SysPostDTO postDTO) {
        SysPost sysPost = new SysPost();
        BeanUtil.copyProperties(postDTO,sysPost);
        return removeById(sysPost);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean updatePost(SysPostDTO postDTO) {
        SysPost sysPost = new SysPost();
        BeanUtil.copyProperties(postDTO,sysPost);
        sysPost.setPostCode(postDTO.getPostCode().toUpperCase());
        sysPost.setTenantId(SecureUtil.userTenantId());
        return updateById(sysPost);
    }
}
